class VideoTest extends eui.Group {
    private static ins: VideoTest = null
    public static get Instance(): VideoTest {
        if (VideoTest.ins == null) {
            VideoTest.ins = new VideoTest()
            VideoTest.ins.top = 0
            VideoTest.ins.left = 0
            VideoTest.ins.bottom = 0
            VideoTest.ins.right = 0
        }
        return VideoTest.ins
    }

    private img_bg: eui.Image
    private mask_video: Video
    private filter: egret.CustomFilter = null
    private mask_texture: egret.Texture = null

    private _video: Video

    childrenCreated() {
        super.childrenCreated()

        this._video = new Video()
        this.addChild(this._video)
        this._video.width = 256
        this._video.height = 256
        this._video.horizontalCenter = 0
        this._video.verticalCenter = 0
        this._video.scaleY=-1
        this._video.Load("resource/assets/1.ts", {
            autoPlay: true,
            onPlay: (video: Video) => {
                // this.onPlay()
            }
        })

        //白鹭自带的video
        // this.mask_video = new Video("./" + RES.getResourceInfo("9c_mp4").root + "" + RES.getResourceInfo("9c_mp4").url, "", 200, 200)
        // this.addChild(this.mask_video)
        // this.mask_video.alpha = 0.01
        // this.mask_video.once("loaded", () => {
        //     this.img_bg = new eui.Image("bg_jpg")
        //     this.img_bg.top = 0
        //     this.img_bg.left = 0
        //     this.img_bg.bottom = 0
        //     this.img_bg.right = 0
        //     this.addChild(this.img_bg)

        //     this.mask_texture = new egret.Texture()
        //     this.mask_texture.bitmapData = this.mask_video.bitmapData

        //     this.filter = new egret.CustomFilter(
        //         RES.getRes("default_vert"),
        //         RES.getRes("video_frag"),
        //         {
        //             iTime: 0,
        //             uNoise: 1,
        //             uMaskTex: 2,
        //         }
        //     )
        //     this.filter["textures"] = {
        //         "TEXTURE1": RES.getRes("Noise_png"),
        //         "TEXTURE2": this.mask_texture,
        //     }
        //     this.img_bg.filters = [this.filter]

        //     this.mask_video.play(0, true)
        // }, this)

        let timeOnEnterFrame = egret.getTimer()
        this.addEventListener(egret.Event.ENTER_FRAME, (e: egret.Event) => {
            let now = egret.getTimer()
            let pass = now - timeOnEnterFrame
            this.update(pass / 1000)
            timeOnEnterFrame = now
        }, this)

        //直接Canvas节点绘制视频
        // let canvas = document.getElementById("video") as HTMLCanvasElement
        // let gl = canvas.getContext('webgl')
        // let tex = gl.createTexture()
        // gl.bindTexture(gl.TEXTURE_2D, tex);
        // gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 256, 256, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

        // var player = new JSMpeg.Player("resource/assets/1.ts", {
        //     loop: true, autoplay: true, canvas: canvas, progressive: false, onVideoDecode: () => {
        //         console.log("onVideoDecode")
        //         // player.play()
        //         // player.nextFrame()


        //         // const fb = gl.createFramebuffer();
        //         // gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
        //     }
        // });
    }

    private update(dt: number) {
        // if (this.filter) {
        //     let iTime = this.filter.uniforms["iTime"]
        //     if (iTime != undefined) {
        //         iTime += dt
        //         this.filter.uniforms["iTime"] = iTime
        //     }

        //     if (this.mask_video.loaded) {
        //         //获取视频的纹理
        //         this.mask_texture.bitmapData = this.mask_video.bitmapData
        //     }
        // }
    }
}